package com.itheima.ydd.service.impl;


import com.itheima.ydd.dto.SysMenuDto;
import com.itheima.ydd.dto.SysRoleDto;
import com.itheima.ydd.entity.SysMenu;
import com.itheima.ydd.entity.SysRole;
import com.itheima.ydd.mapper.SysMenuMapper;
import com.itheima.ydd.mapper.SysRoleMapper;
import com.itheima.ydd.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    private SysMenuMapper menuMapper;

    @Autowired
    private ISysRoleService roleService;
    public SysRoleDto rowInfo(String roleId) {
        SysRole sysRole = roleService.getById(roleId);
        SysRoleDto roleDto = new SysRoleDto();
        BeanUtils.copyProperties(sysRole, roleDto);

        //查询根级菜单
        List<SysMenu> menuList = menuMapper.selectMenu(roleId,0);

        //查询菜单
        List<SysMenuDto> sysMenuDtos = menuList.stream().map(menu -> {
            SysMenuDto menuDto = new SysMenuDto();
            BeanUtils.copyProperties(menu, menuDto);

            this.getMenuDto(roleId, menuDto);

            return menuDto;
        }).collect(Collectors.toList());
        roleDto.setAuthList(sysMenuDtos);
        return roleDto;
    }
    private void getMenuDto(String roleId, SysMenuDto menuDto) {
        Integer id = menuDto.getId();
        List<SysMenu> sysMenus = menuMapper.selectMenu(roleId, id);
        if (sysMenus.isEmpty()) {
            return;
        }
        List<SysMenuDto> sysMenuDtos = sysMenus.stream().map(menu -> {
            SysMenuDto dto = new SysMenuDto();
            BeanUtils.copyProperties(menu, dto);

            getMenuDto(roleId,dto);
            return dto;
        }).collect(Collectors.toList());
        menuDto.setChildren(sysMenuDtos);
    }
}
